package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.FunctionItemBean;

/**
 * 普通功能项适配器
 */
public final class FunctionItemAdapter extends AppAdapter<FunctionItemBean> {

    private OnAddClickListener mAddListener;
    private OnItemClickListener mItemClickListener;
    private boolean isEditMode = true; // 默认为编辑模式

    public FunctionItemAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FunctionHolder();
    }

    public void setOnAddClickListener(OnAddClickListener listener) {
        this.mAddListener = listener;
    }
    
    /**
     * 设置Item点击监听（用于查看模式）
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
    
    /**
     * 设置是否为编辑模式
     * @param isEditMode true-编辑模式（显示加号），false-查看模式（隐藏加号，可点击跳转）
     */
    public void setEditMode(boolean isEditMode) {
        this.isEditMode = isEditMode;
    }

    private final class FunctionHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView ivIcon;
        private final TextView tvName;
        private final ImageView ivAdd;

        private FunctionHolder() {
            super(R.layout.function_item_normal);
            ivIcon = findViewById(R.id.iv_function_icon);
            tvName = findViewById(R.id.tv_function_name);
            ivAdd = findViewById(R.id.iv_add);
        }

        @Override
        public void onBindView(int position) {
            FunctionItemBean item = getItem(position);
            ivIcon.setImageResource(item.getIconRes());
            tvName.setText(item.getName());
            
            if (isEditMode) {
                // 编辑模式：显示/隐藏加号，点击添加
                ivAdd.setVisibility(item.isAdded() ? View.GONE : View.VISIBLE);

                // 设置点击事件
                ivAdd.setOnClickListener(v -> {
                    if (mAddListener != null) {
                        mAddListener.onAddClick(position, item);
                    }
                });

                // 整个item也可以点击添加
                getItemView().setOnClickListener(v -> {
                    if (!item.isAdded() && mAddListener != null) {
                        mAddListener.onAddClick(position, item);
                    }
                });
            } else {
                // 查看模式：隐藏加号，点击跳转
                ivAdd.setVisibility(View.GONE);
                
                // 整个item点击跳转
                getItemView().setOnClickListener(v -> {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(position, item);
                    }
                });
            }
        }
    }

    public interface OnAddClickListener {
        void onAddClick(int position, FunctionItemBean item);
    }
    
    public interface OnItemClickListener {
        void onItemClick(int position, FunctionItemBean item);
    }
}

